package com.atguigu.web;

import com.atguigu.Bean.C_cart;
import com.atguigu.Bean.C_myOrder;
import com.atguigu.Bean.C_page;
import com.atguigu.Bean.C_user;
import com.atguigu.service.OrderService;
import com.atguigu.service.impl.OrderServiceImpl;
import com.atguigu.utils.JdbcUtils;
import com.atguigu.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class OrderServlet extends BaseServlet{
    OrderService os=new OrderServiceImpl();
    /**
     * 生成订单
     */
    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先获取cart购物车对象
        C_cart c_cart = (C_cart) req.getSession(false).getAttribute("cart");
        //获取用户登陆对象
        C_user user = (C_user) req.getSession().getAttribute("user");
        //判断是否登录
        if (user==null){
        /*未登录*/
            //请求转发到登陆页面
            req.getRequestDispatcher("pages/UserPages/login.jsp").forward(req,resp);
        }
        else{
        /*已登录*/
            //获取用户id
            Integer id = user.getId();
            //调用OrderService.createOrder()方法生成订单
            String orderId = os.createOrder(c_cart,id);
            //将订单号保存到session域中
            req.getSession().setAttribute("orderId",orderId);
            //请求转发到新页面
            //req.getRequestDispatcher("pages/UserPages/BusinessPages/PaymentSuccessful.jsp").forward(req,resp);
            //请求重定向到新页面
            resp.sendRedirect(req.getContextPath()+"/pages/UserPages/BusinessPages/PaymentSuccessful.jsp");
        }
    }

    /**
     * 查看我的订单
     */
    protected void showMyOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        C_user user = (C_user) req.getSession().getAttribute("user");
        //判断是否登录
        if(user==null){
            //未登录直接转到登录页面
            req.getRequestDispatcher("pages/UserPages/login.jsp").forward(req,resp);
        }
        else {
            //登录则进行以下操作
            //由于使用了分页操作，这里得到当前页码和每页数据行数
            int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
            int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), C_page.PAGE_SIZE);
            //取得要展示订单详情的用户的id值
            Integer id = Integer.valueOf(req.getParameter("userId"));
            C_page<C_myOrder> page = os.showMyOrders(id, pageNo, pageSize);
            //设置和保存url用于分页操作的页面跳转
            page.setUrl("os?action=showMyOrders&userId="+id);
            req.setAttribute("page",page);
            //保存要展示订单详情的用户的id值,用于分页操作的页面跳转
            req.setAttribute("userId",id);
            req.getRequestDispatcher("/pages/UserPages/BusinessPages/MyOrder.jsp").forward(req,resp);
        }

    }
    /**
     * 查看所有有订单的用户(管理员权限)
     */
    protected void showAllOrderUsers(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        C_user user = (C_user) req.getSession().getAttribute("user");
        //判断是否登录
        if(user==null){
            //未登录直接转到登录页面
            req.getRequestDispatcher("pages/UserPages/login.jsp").forward(req,resp);
        }
        else {
            //登录则进行以下操作
            //由于使用了分页操作，这里得到当前页码和每页数据行数
            int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
            int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), C_page.PAGE_SIZE);
            C_page<C_user> page = os.showAllOrderUsers(pageNo, pageSize);
            //设置和保存url用于分页操作的页面跳转
            page.setUrl("os?action=showAllOrderUsers");
            req.setAttribute("page",page);
            req.getRequestDispatcher("/pages/ManagerPages/c_game_orders_manager.jsp").forward(req,resp);
        }
    }
    /**
     * 修改用户的订单状态为发货(管理员权限)
     */
    protected void sendOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderId = req.getParameter("orderId");
        os.sendOrder(orderId);
        String referer = req.getHeader("Referer");
        resp.sendRedirect(referer);
    }
    /**
     * 修改用户的订单状态为签收(用户权限)
     */
    protected void receiverOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderId = req.getParameter("orderId");
        os.receiverOrder(orderId);
        String referer = req.getHeader("Referer");
        resp.sendRedirect(referer);
    }
}
